﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Escuela.Forms
{
    public partial class CierreDiarioForm : Form
    {
        private Instituto inst;
        protected int acep;
        public CierreDiarioForm(Instituto inst)
        {
            this.inst = inst;
            
            InitializeComponent();
        }


        private void Nuevo_Click(object sender, EventArgs e)
        {
            try
            {
                acep = 1;
                dateTimePicker1.Enabled = false;
                disableBtn();
                limpiarBox();
                maskedNroCur.Enabled = false;

                DateTime fechaActual = Convert.ToDateTime(dateTimePicker1.Text);
                CierreDiario cd = this.inst.registrarCierreDiario(fechaActual);

                decimal monto = cd.MontoTotal;
                this.maskedNroCur.Text = Convert.ToString(monto);
                this.enableTxt();       
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error al realizar el cierre" + ex.Message);
            }

        }

        private void Confirmar_Click(object sender, EventArgs e)
        {
            switch (acep)
            {
                #region Confirmar
                case 1:
                try
                {
                    if (!String.IsNullOrEmpty(maskedTextBox1.Text.Trim()) && !String.IsNullOrEmpty(textDescrip.Text.Trim()))
                    {

                        DateTime fechaActual = Convert.ToDateTime(dateTimePicker1.Text.ToString().Trim());
                        decimal diferencia = Convert.ToDecimal(this.maskedTextBox1.Text.Trim());
                        string observacion = this.textDescrip.Text.Trim();
                        CierreDiario cd = this.inst.completarDatosCierreDiario(diferencia, observacion, fechaActual);
                        this.inst.guardarCierreDiario(cd);
                        if (cd != null)
                        {
                            MessageBox.Show("Cierre finalizado con exito");
                            //this.coop_cierre_diario_GetAllTableAdapter.Fill(this.dataSet7.coop_cierre_diario_GetAll);
                            this.coop_cierre_diario_GetAllTableAdapter1.Fill(this.escuelaDataSet.coop_cierre_diario_GetAll);
                            this.limpiarBox();
                            this.enableBtn();
                            this.disableTxt();

                        }
                    }
                    else
                    {
                        MessageBox.Show("Complete toda la informacion solicitada");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error al realizar el cierre" + ex.Message);
                }                    
                break;

                #endregion

                #region Buscar
                case 2:
                    
                    try
                    {
                        
                        string where = "fecha= ' " + this.dateTimePicker1.Value;
                        if (this.dateTimePicker1.Text != null && this.dateTimePicker1.Text.Trim() != "")
                        {
                            where = where + "fecha= " + this.dateTimePicker1.Text.Trim().ToString();
                            
                        }
                        

                        if (where.Trim().Length > 3)
                            where = where.Substring(0, where.Length - 5);

                        coopcierrediarioGetAllBindingSource.Filter = where; 
                    
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error al buscar cierre" + ex.Message);
                    }
                    break;

                    limpiarBox();


                #endregion
            }

        }

        //private void Cancelar_Click(object sender, EventArgs e)
        //{
        //    limpiarBox();
        //    disableTxt();
        //    enableBtn();
        //    //coop_cierre_diario_GetAllTableAdapter.RemoveFilter();
        //}

        private void Buscar_Click(object sender, EventArgs e)
        {
            try
            {
                acep = 2;
                disableBtn();
                dateTimePicker1.Enabled = true;
                limpiarBox();

            }
            catch (Exception ex)
            {
                MessageBox.Show("Se produjo un error al Habilitar campo para nuevo Cierre" + ex.Message);
            }
        }

        protected void limpiarBox()
        {
            this.maskedNroCur.Clear();
            this.maskedTextBox1.Clear();
            this.textDescrip.Clear();
        }

        protected void enableTxt()
        {
            this.maskedTextBox1.Enabled = true;
            this.textDescrip.Enabled = true;
        }

        protected void disableTxt()
        {
            this.maskedNroCur.Enabled = false;
            this.maskedTextBox1.Enabled = false;
            this.textDescrip.Enabled = false;
        }
        protected void disableBtn()
        {
            this.Nuevo.Enabled = false;
            this.Confirmar.Enabled = true;
            this.Buscar.Enabled = false;
        }

        protected void enableBtn()
        {
            this.Nuevo.Enabled = true;
            this.Confirmar.Enabled = false;
            this.Buscar.Enabled = true;
        }

        private void CierreDiarioForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'escuelaDataSet.coop_cierre_diario_GetAll' table. You can move, or remove it, as needed.
            this.coop_cierre_diario_GetAllTableAdapter1.Fill(this.escuelaDataSet.coop_cierre_diario_GetAll);
            // TODO: This line of code loads data into the 'dataSet7.coop_cierre_diario_GetAll' table. You can move, or remove it, as needed.
            //this.coop_cierre_diario_GetAllTableAdapter.Fill(this.dataSet7.coop_cierre_diario_GetAll);

        }

     

        

     

    }
}
